<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Radio Simulator Agent</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            height: 100vh;
            display: flex;
            flex-direction: column;
        }
        
        .header {
            background-color: #2c3e50;
            color: white;
            padding: 1rem;
            text-align: center;
        }
        
        .chat-area {
            flex: 1;
            overflow-y: auto;
            padding: 1rem;
            background-color: white;
            margin: 1rem;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        .message {
            margin-bottom: 1rem;
            padding: 0.5rem;
            border-radius: 4px;
            max-width: 80%;
        }
        
        .user-message {
            background-color: #3498db;
            color: white;
            margin-left: auto;
        }
        
        .assistant-message {
            background-color: #ecf0f1;
            color: #2c3e50;
        }
        
        .input-area {
            display: flex;
            padding: 1rem;
            background-color: white;
            margin: 1rem;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        #user-input {
            flex: 1;
            padding: 0.5rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 1rem;
        }
        
        #send-button {
            margin-left: 0.5rem;
            padding: 0.5rem 1rem;
            background-color: #2c3e50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1rem;
        }
        
        #send-button:hover {
            background-color: #1a252f;
        }
        
        .typing-indicator {
            display: none;
            color: #7f8c8d;
            font-style: italic;
            padding: 0.5rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>Radio Simulator Agent</h1>
        </div>
        
        <div class="chat-area" id="chat-area">
            <div class="message assistant-message">
                您好！我是射电天文仿真软件助手。您可以询问关于软件结构的说明，或者让我帮您运行观测模拟。请问有什么可以帮助您的吗？
            </div>
        </div>
        
        <div class="typing-indicator" id="typing-indicator">
            正在输入...
        </div>
        
        <div class="input-area">
            <input type="text" id="user-input" placeholder="请输入您的问题..." autocomplete="off">
            <button id="send-button">发送</button>
        </div>
    </div>

    <script>
        const chatArea = document.getElementById('chat-area');
        const userInput = document.getElementById('user-input');
        const sendButton = document.getElementById('send-button');
        const typingIndicator = document.getElementById('typing-indicator');
        
        // 添加消息到聊天区域
        function addMessage(content, isUser = false) {
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${isUser ? 'user-message' : 'assistant-message'}`;
            messageDiv.textContent = content;
            chatArea.appendChild(messageDiv);
            chatArea.scrollTop = chatArea.scrollHeight;
        }
        
        // 显示输入指示器
        function showTypingIndicator() {
            typingIndicator.style.display = 'block';
            chatArea.scrollTop = chatArea.scrollHeight;
        }
        
        // 隐藏输入指示器
        function hideTypingIndicator() {
            typingIndicator.style.display = 'none';
        }
        
        // 发送消息
        async function sendMessage() {
            const message = userInput.value.trim();
            if (!message) return;
            
            // 添加用户消息
            addMessage(message, true);
            userInput.value = '';
            
            // 显示输入指示器
            showTypingIndicator();
            
            try {
                // 调用后端API
                const response = await fetch('/chat', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({message: message})
                });
                
                hideTypingIndicator();
                
                // 处理流式响应
                const reader = response.body.getReader();
                const decoder = new TextDecoder();
                
                const messageDiv = document.createElement('div');
                messageDiv.className = 'message assistant-message';
                chatArea.appendChild(messageDiv);
                
                let fullText = '';
                while (true) {
                    const {done, value} = await reader.read();
                    if (done) break;
                    
                    const chunk = decoder.decode(value, {stream: true});
                    fullText += chunk;
                    messageDiv.textContent = fullText;
                    chatArea.scrollTop = chatArea.scrollHeight;
                }
            } catch (error) {
                hideTypingIndicator();
                addMessage("抱歉，处理您的请求时出现错误。");
            }
        }
        
        // 事件监听器
        sendButton.addEventListener('click', sendMessage);
        
        userInput.addEventListener('keypress', (e) => {
            if (e.key === 'Enter') {
                sendMessage();
            }
        });
        
        // 聚焦输入框
        userInput.focus();
    </script>
</body>
</html>